package com.woniu.carrent.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入时填充创建时间和结束时间
        Object creattime = getFieldValByName("creattime", metaObject);
        if (creattime == null) {
            this.strictInsertFill(metaObject, "creattime", Date.class, new Date());
        }

        // 插入时如果 endTime 为空，则设置为当前时间
        Object endtime = getFieldValByName("endtime", metaObject);
        if (endtime == null) {
            this.strictInsertFill(metaObject, "endtime", Date.class, new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时如果 endTime 为空，则设置为当前时间
        Object endtime = getFieldValByName("endtime", metaObject);
        if (endtime == null) {
            this.strictUpdateFill(metaObject, "endtime", Date.class, new Date());
        }
    }
}
